<!--
    @license
    Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
    This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
    The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
    The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
    Code distributed by Google as part of the polymer project is also
    subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../components/polymer/polymer.html">
<link rel="import" href="doc-page.html">

<polymer-element name="component-docs" attributes="bannerHeight elements data">
  <template>
    
    <style>
    :host {
      display: block;
    }
    </style>

    <doc-page id="{{doc.name}}" name="{{doc.name}}" data="{{doc}}"
                bannerHeight="{{bannerHeight}}"
                downloadable?="{{isTopLevelElement(doc.name, doc.location)}}"></doc-page>

  </template>
  <script>
    Polymer({
      // Default to the height of the banner on the current site, though this can
      // be overridden by the page that uses <compontent-docs> as needed.
      bannerHeight: 80,
      data: {},
      dataChanged: function(oldVal, newVal) {
        this.doc = this.sanitize_(newVal);
      },
      /**
       * Fix escaped closing script tags in descriptions
       */
      sanitize_: function(data) {
        data.description = data.description.replace(
          '<\\/script>', '<\/script>'
        );
        return data;
      },
      /**
       * Check if element is top level or nested inside another element's
       * directory structure. This affects whether or not a demo and download
       * buttuon will be shown for it in the docs page
       */
      isTopLevelElement: function(name, url) {
        if (!name || !url) {
          return;
        }
        // Will take the url 0.5/components/core-ajax/core-ajax.html
        // and return components/core-ajax/core-ajax.html
        var versionFreeUrl = url.split('/').slice(1).join('/');
        return versionFreeUrl.indexOf('components/' + name + '/') == 0;
      }
    });
  </script>
</polymer-element>
